﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Main_Accounts_VoucherPrint_Contra_print : System.Web.UI.Page
{
    double grandtotal_dr = 0;
    double grandtotal_cr = 0;
    Datautility du = new Datautility();
    DataTable DTMain = new DataTable();
    string strsql = "";
    string currentyear = "";
    string Nextyear = "";
    string currentFyear = "";
    string transactiontype = "";

    DataSet ds = new DataSet();
    NumToWords nw = new NumToWords();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (Request.QueryString["key"] != null)
        {
            string fromdate = "";
            string fromdate1 = "";
            string todate = "";
            string todate1 = "";
            string tranid = "";

            tranid = Request.QueryString["key"].ToString();

            fromdate = Request.QueryString["from"].ToString();
            string[] parma = fromdate.Split('/');
            fromdate1 = parma[1] + "/" + parma[0] + "/" + parma[2];

            todate = Request.QueryString["to"].ToString();
            string[] parma1 = todate.Split('/');
            todate1 = parma1[1] + "/" + parma1[0] + "/" + parma1[2];

            if (Request.QueryString["transactiontype"] != null)
            {
                transactiontype = Request.QueryString["transactiontype"].ToString();
                DataTable dttran_name = du.getdata("select transactionname from ACC_TRANSETTINGS where typeid=" + Request.QueryString["transactiontype"].ToString() + "");
                if (dttran_name.Rows.Count > 0)
                {
                    if (dttran_name.Rows[0]["transactionname"].ToString() != string.Empty)
                    {
                        lbltransactiontype.Text = dttran_name.Rows[0]["transactionname"].ToString() + " Voucher";
                    }
                }
            }

            if (!IsPostBack)
            {
                BindDataList(fromdate1,todate1,tranid);

                DataTable dtcompanydetails = du.getdatausingprocedure("usp_get_company_details");
                if (dtcompanydetails.Rows.Count > 0)
                {
                    if (dtcompanydetails.Rows[0]["compname"].ToString() != string.Empty)
                    {
                        lblcompanyname.Text = dtcompanydetails.Rows[0]["compname"].ToString();
                    }
                    if (dtcompanydetails.Rows[0]["add1"].ToString() != string.Empty)
                    {
                        lbladd1.Text = dtcompanydetails.Rows[0]["add1"].ToString();
                    }
                    if (dtcompanydetails.Rows[0]["add2"].ToString() != string.Empty)
                    {
                        lbladd2.Text = dtcompanydetails.Rows[0]["add2"].ToString();
                    }
                    if (dtcompanydetails.Rows[0]["add3"].ToString() != string.Empty)
                    {
                        lbladd3.Text = dtcompanydetails.Rows[0]["add3"].ToString();
                    }
                    if (dtcompanydetails.Rows[0]["add4"].ToString() != string.Empty)
                    {
                        lbladd4.Text = dtcompanydetails.Rows[0]["add4"].ToString();
                    }
                    if (dtcompanydetails.Rows[0]["place"].ToString() != string.Empty)
                    {
                        lblplace.Text = dtcompanydetails.Rows[0]["place"].ToString();
                    }
                    if (dtcompanydetails.Rows[0]["pincode"].ToString() != string.Empty)
                    {
                        lblpinno.Text = dtcompanydetails.Rows[0]["pincode"].ToString();
                    }
                    if (dtcompanydetails.Rows[0]["pan"].ToString() != string.Empty)
                    {
                        lblpanname.Text = "Pan No. ";
                        panno.Text = dtcompanydetails.Rows[0]["pan"].ToString();
                    }
                    if (dtcompanydetails.Rows[0]["vatno"].ToString() != string.Empty)
                    {
                        lblvatname.Text = "Vat No. ";
                        lblvatno.Text = dtcompanydetails.Rows[0]["vatno"].ToString();
                    }
                   
                }
            }
        }

      
    }
    
    protected void BindDataList(string fromdate, string todate, string tranid)
    {


        SqlParameter[] parma ={
                               new SqlParameter("@TransactionTypeID",transactiontype), // 1 for Contra
                               new SqlParameter("@FromDate",fromdate),
                               new SqlParameter("@todate",todate),
                               new SqlParameter("@Tranid",tranid)
                              };
        DTMain = du.getdatausingprocedure("usp_acc_getvoucherviewdetail_by_TransID", parma);
        if (DTMain.Rows.Count > 0)
        {
            
            DataTable DT = new DataTable();
            string[] columns = {"TransactionId", "TransactionDate", "TransactionNumber", "ChequeNumber", "Narration"};
            DT = DTMain.DefaultView.ToTable(true, columns);
            dtlcontra.DataSource = DT;
            dtlcontra.DataBind();
        }
        else
        {
           
        }
    }

    protected void dtlcontra_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        if (Request.QueryString["key"] != null)
        {
            string fromdate = "";
            string fromdate1 = "";
            string todate = "";
            string todate1 = "";
            string tranid = "";

            tranid = Request.QueryString["key"].ToString();

            fromdate = Request.QueryString["from"].ToString();
            string[] parma = fromdate.Split('/');
            fromdate1 = parma[1] + "/" + parma[0] + "/" + parma[2];

            todate = Request.QueryString["to"].ToString();
            string[] parma1 = todate.Split('/');
            todate1 = parma1[1] + "/" + parma1[0] + "/" + parma1[2];

            SqlParameter[] parma22 ={
                               new SqlParameter("@TransactionTypeID",transactiontype), // 1 for Contra
                               new SqlParameter("@FromDate",fromdate1),
                               new SqlParameter("@todate",todate1),
                               new SqlParameter("@Tranid",tranid)
                              };
            DTMain = du.getdatausingprocedure("usp_acc_getvoucherviewdetail_by_TransID", parma22);
            //if (DTMain.Rows.Count > 0)
            //{
            //    GridView gridcontra = (GridView)e.Item.FindControl("gridcontra");
            //    gridcontra.DataSource = DTMain;
            //    gridcontra.DataBind();
            //}
            DataTable DTDisplayDetail = new DataTable();
            DTDisplayDetail.Columns.Add("particulars", typeof(string));
            DTDisplayDetail.Columns.Add("Debit", typeof(string));
            DTDisplayDetail.Columns.Add("Credit", typeof(string));
            DTDisplayDetail.Columns.Add("TransactionSerial", typeof(string));
            for (int i = 0; i < DTMain.Rows.Count; i++)
            {
                DataRow DR = DTDisplayDetail.NewRow();

                double amount = Convert.ToDouble(DTMain.Rows[i]["Amount"].ToString());
                if (amount < 0 && amount.ToString() != string.Empty)
                {
                    DR["Debit"] = string.Format("{0:0,0.00}", Math.Abs(amount));
                    DR["Credit"] = string.Empty;
                    DR["particulars"] = DTMain.Rows[i]["particulars"]; ;
                }
                else
                {
                    DR["Credit"] = string.Format("{0:0,0.00}", Math.Abs(amount));
                    DR["Debit"] = string.Empty;
                    DR["particulars"] = "<i>To</i> " + DTMain.Rows[i]["particulars"];
                }
                DR["TransactionSerial"] = DTMain.Rows[i]["TransactionSerial"].ToString();
                DTDisplayDetail.Rows.Add(DR);
            }
            int new_count = 12 - DTMain.Rows.Count;
            for (int i = 0; i < new_count; i++)
            {
                DataRow DR = DTDisplayDetail.NewRow();
                DR["particulars"] = string.Empty;
                DTDisplayDetail.Rows.Add(DR);
            }
            DTDisplayDetail.Rows[DTDisplayDetail.Rows.Count - 4]["particulars"] = "<b>On Account of :</b>";
            DTDisplayDetail.Rows[DTDisplayDetail.Rows.Count - 3]["particulars"] = DTMain.Rows[0]["narration"];
            DTDisplayDetail.Rows[DTDisplayDetail.Rows.Count - 2]["particulars"] = "<b>Amount (in words) :</b>";
            GridView gridcontra = (GridView)e.Item.FindControl("gridcontra");
            gridcontra.DataSource = DTDisplayDetail;
            gridcontra.DataBind();
        }
    }
    protected void gridContra_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        GridView grd = (GridView)sender;
        DataTable DTSum = new DataTable();
        DTSum.Columns.Add("Debit", typeof(double));
        DTSum.Columns.Add("Credit", typeof(double));
        DataTable dt = (DataTable)grd.DataSource;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = DTSum.NewRow();
            if (dt.Rows[i]["Debit"].ToString() == string.Empty)
            {
                dr["Debit"] = 0;
            }
            else
            {
                dr["Debit"] = dt.Rows[i]["Debit"].ToString();
            }

            if (dt.Rows[i]["Credit"].ToString() == string.Empty)
            {
                dr["Credit"] = 0;
            }
            else
            {
                dr["Credit"] = dt.Rows[i]["Credit"].ToString();
            }
            DTSum.Rows.Add(dr);
        }
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Style["text-align"] = "left";
            e.Row.Cells[0].Style["border-right"] = "1px solid black;";
            e.Row.Cells[0].Style["border-bottom"] = "1px solid black;";
            e.Row.Cells[1].Style["border-right"] = "1px solid black;";
            e.Row.Cells[0].Style["border-top"] = "1px solid black;";
            e.Row.Cells[1].Style["border-top"] = "1px solid black;";
            e.Row.Cells[1].Style["border-bottom"] = "1px solid black;";
            e.Row.Cells[2].Style["border-top"] = "1px solid black;";
            e.Row.Cells[2].Style["border-bottom"] = "1px solid black;";
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Style["border-right"] = "1px solid black;";
            e.Row.Cells[1].Style["border-right"] = "1px solid black;";
            e.Row.Cells[0].Style["text-align"] = "left";
            if (e.Row.RowIndex == DTSum.Rows.Count - 1)
            {
                double sum = (double)DTSum.Compute("sum(Debit)", "");
                //e.Row.Cells[0].Text = nw.ToWords(sum);
            }
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[0].Style["border"] = "0px solid white;";
            e.Row.Cells[0].Style["border-right"] = "1px solid black;";

            e.Row.Cells[1].Text = string.Format("{0:0,0.00}", Convert.ToDouble(DTSum.Compute("sum(Debit)", "").ToString()));
            e.Row.Cells[1].Style["text-align"] = "right";
            e.Row.Cells[1].Style["border"] = "1px solid black";
            e.Row.Cells[1].Style["border-left"] = "0px solid white";

            e.Row.Cells[2].Text = string.Format("{0:0,0.00}", Convert.ToDouble(DTSum.Compute("sum(Credit)", "").ToString()));
            e.Row.Cells[2].Style["text-align"] = "right";
            e.Row.Cells[2].Style["border"] = "1px solid black";
        }


        //GridView grdBind = (GridView)sender;
        //DataListItem dlItem = (DataListItem)grdBind.Parent;

        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    Literal debit = (Literal)e.Row.Cells[1].FindControl("ltrdebit");
        //    Literal credit = (Literal)e.Row.Cells[2].FindControl("ltrcredit");

        //    string dk = grdBind.DataKeys[e.Row.RowIndex].Value.ToString();
        //    strsql = "select amount from acc_voudetails where vouserial='" + dk.ToString() + "'";
        //    DataTable DT = du.getdata(strsql);
        //    //if (DT.Rows.Count > 0)
        //    //{
        //    //    if (DT.Rows[0]["amount"].ToString() != string.Empty)
        //    //    {
        //    //        //Literal dr = (Literal)e.Row.Cells[1].FindControl("ltrdr_cr");
        //    //        if (Convert.ToDouble(DT.Rows[0]["amount"].ToString()) > 0)
        //    //        {
                       
        //    //        }
        //    //        else
        //    //        {
                        
        //    //        }

        //    //    }

        //    //}
        //    //if (debit.Text != string.Empty)
        //    //{
        //    //    if (Convert.ToDouble(debit.Text) == 0)
        //    //    {
        //    //        debit.Text = "";
        //    //    }
        //    //    else
        //    //    {
        //    //        debit.Text = Math.Abs(Convert.ToDouble(debit.Text)).ToString("N2");
        //    //        grandtotal_dr += Convert.ToDouble(debit.Text);

        //    //    }
        //    //}
        //    //if (credit.Text != string.Empty)
        //    //{
        //    //    if (Convert.ToDouble(credit.Text) == 0)
        //    //    {
        //    //        credit.Text = "";
        //    //    }
        //    //    else
        //    //    {
        //    //        credit.Text = Math.Abs(Convert.ToDouble(credit.Text)).ToString("N2");
        //    //        grandtotal_cr += Convert.ToDouble(credit.Text);
        //    //        e.Row.Cells[2].Text = nw.ToWords(grandtotal_cr);
        //    //    }
        //    //}

        //    e.Row.Cells[0].Style["border-right"] = "1px solid black;";
        //    e.Row.Cells[1].Style["border-right"] = "1px solid black;";
        //    e.Row.Cells[0].Style["text-align"] = "left";

        //}
        //if (e.Row.RowType == DataControlRowType.Footer)
        //{
        //    ((Literal)e.Row.FindControl("ltrtotdr")).Text = grandtotal_dr.ToString("N2");
        //    ((Literal)e.Row.FindControl("ltrtotcr")).Text = grandtotal_cr.ToString("N2");
        //    e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
        //    e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;

        //    e.Row.Cells[0].Style["border"] = "0px solid white;";
        //    e.Row.Cells[0].Style["border-right"] = "1px solid black;";
        //    e.Row.Cells[1].Style["border"] = "1px solid black";
        //    e.Row.Cells[1].Style["border-left"] = "0px solid white";
        //    e.Row.Cells[2].Style["border"] = "1px solid black";
        //}
        //if (e.Row.RowType == DataControlRowType.Header)
        //{
        //    e.Row.Cells[0].Style["text-align"] = "left";
        //    e.Row.Cells[0].Style["border-right"] = "1px solid black;";
        //    e.Row.Cells[1].Style["border-right"] = "1px solid black;";
        //    e.Row.Cells[0].Style["border-top"] = "1px solid black;";
        //    e.Row.Cells[1].Style["border-top"] = "1px solid black;";
        //    e.Row.Cells[2].Style["border-top"] = "1px solid black;";
        //}
    }
}
